package com.jeffers.videorecord;

import android.hardware.Camera;
import android.util.Log;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

// reference blog
// http://blog.csdn.net/yanzi1225627/article/details/17652643
public class CameraSizeUtils {

	private static final String TAG = "CameraSizeUtils";


	public static Camera.Size getPictureSize(List<Camera.Size> list,int th){
		Collections.sort(list, new SizeComparator());

		int i = 0;
		for(Camera.Size s:list){
			if((s.width > th) && equalRate(s, 1.33f)){
				Log.i(TAG, "End Setting  Size :w = " + s.width + " , h = " + s.height);
				break;
			}
			i++;
		}
		return list.get(i);
	}

	public static boolean equalRate(Camera.Size s, float rate){
		float r = (float)(s.width)/(float)(s.height);
		if(Math.abs(r - rate) <= 0.2){
			return true;
		}
		return false;
	}

	// asc sort
	public static class SizeComparator implements Comparator<Camera.Size> {
		@Override
		public int compare(Camera.Size lhs, Camera.Size rhs) {
			if(lhs.width == rhs.width){
				return 0;
			}else if(lhs.width > rhs.width){
				return 1;
			}
			return -1;
		}
	}
}
